package com.tarena.lbs.content.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.enums.Roles;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.content.pojo.param.ArticleActionParam;
import com.tarena.lbs.content.pojo.param.ArticleContentParam;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleActionPageResultVO;
import com.tarena.lbs.content.pojo.vo.ArticleVO;
import com.tarena.lbs.content.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * 文章业务模块接口
 */
@Slf4j
@RestController
@Tag(name = "文章相关功能接口")
@Validated
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Operation(summary = "文章分页列表查询")
    @GetMapping("/admin/content/article/list")
    public Result<PageResult<ArticleVO>> pageList(ArticleQuery articleQuery)
            throws BusinessException {
        log.debug("[控制层]处理文章分页列表查询,入参{}", articleQuery);
        //鉴权并获取当前用户
        UserPrinciple userPrinciple = checkUser();
        //调用
        return new Result("0", "success", articleService.pageList(articleQuery));
    }


    @Operation(summary = "文章保存")
    @PostMapping("/admin/content/article/insert")
    public Result<Void> addArticle(@Valid ArticleContentParam param)
            throws BusinessException {
        log.debug("[控制层]处理文章保存,入参{}", param);
        //鉴权并获取当前用户
        UserPrinciple userPrinciple = checkUser();
        //调用
        int rows = articleService.addArticle(param);
        return rows > 0 ? Result.success() : Result.fail();
    }

    @Operation(summary = "文章更新")
    @PostMapping("/admin/content/article/update")
    public Result<Void> updateArticle(@Valid ArticleContentParam param)
            throws BusinessException {
        log.debug("[控制层]处理文章更新,入参{}", param);
        //鉴权并获取当前用户
        UserPrinciple userPrinciple = checkUser();
        //调用
        int rows = articleService.updateArticle(param);
        return rows > 0 ? Result.success() : Result.fail();
    }

    @Operation(summary = "文章详情")
    @GetMapping("/admin/content/article/get")
    public Result<ArticleVO> articleDetail(@NotBlank(message = "文章ID不能为空") String id) throws BusinessException {
        log.debug("[控制层]处理文章详情查询,入参{}", id);
        //鉴权并获取当前用户
        UserPrinciple userPrinciple = checkUser();
        //调用
        return new Result("0", "success", articleService.articleDetail(id));
    }

    @Operation(summary = "文章删除")
    @PostMapping("/admin/content/article/delete")
    public Result<Void> deleteArticle(@NotBlank(message = "文章ID不能为空") String id) throws BusinessException {
        log.debug("[控制层]处理文章删除,入参{}", id);
        //鉴权并获取当前用户
        UserPrinciple userPrinciple = checkUser();
        //调用
        int rows = articleService.deleteArticle(id);
        return rows > 0 ? Result.success() : Result.fail();
    }

    @Operation(summary = "文章标签集合")
    @GetMapping("/admin/content/article/getArticleLabel")
    public Result<Set<String>> articleLabels(@Valid ArticleQuery articleQuery) throws BusinessException {
        log.debug("[控制层]处理文章标签集合查询,入参{}", articleQuery);
        //鉴权并获取当前用户
        UserPrinciple userPrinciple = checkUser();
        //调用
        return new Result("0", "success", articleService.articleLabels(articleQuery));
    }

    @Operation(summary = "用户行为记录")
    @PostMapping("/admin/content/article/behavior")
    public Result<Void> articleBehavior(@Valid ArticleActionParam param)
            throws BusinessException {
        log.debug("[控制层]处理用户行为记录,入参{}", param);
        //鉴权并获取当前用户
        UserPrinciple userPrinciple = checkUser();
        //调用
        int rows = articleService.articleBehavior(param);
        return rows > 0 ? Result.success() : Result.fail();
    }


    @Operation(summary = "查询已经点赞 收藏 访问过的文章")
    @GetMapping("/admin/content/article/getBehaviorList")
    public Result<ArticleActionPageResultVO> getBehaviorLists(@Valid ArticleQuery query)
            throws BusinessException {
        log.debug("[控制层]处理用户行为文章列表查询,入参{}", query);
        //鉴权并获取当前用户
        UserPrinciple userPrinciple = checkUser();
        //调用
        return new Result("0", "success", articleService.getBehaviorLists(query));
    }

    private UserPrinciple checkUser() throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        if (userPrinciple == null) {
            throw new BusinessException("401", "用户未登录");
        } else if (!(userPrinciple.getRole() == Roles.SHOP || userPrinciple.getRole() == Roles.ADMIN)) {
            throw new BusinessException("403", "用户无权限");
        }
        log.debug("[控制层]处理用户鉴权成功");
        return userPrinciple;
    }
}
